package com.fling.background;

import java.util.ArrayList;

import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.fling.Locator;

public class BackgroundBlockManager {

	private ArrayList<BackgroundBlockRow> rows;
	private ArrayList<BackgroundBlockRow> deleted;
	private float timer;
	private boolean useOverallSpeed;
	public float CALCULATED_TIME = 5f;

	public BackgroundBlockManager(boolean speed) {
		rows = new ArrayList<BackgroundBlockRow>();
		deleted = new ArrayList<BackgroundBlockRow>();
		timer = 5;
		useOverallSpeed = speed;
		fillScreen();
	}

	public void render(SpriteBatch sb) {
		for (BackgroundBlockRow bbr : rows) {
			bbr.render(sb);
		}
	}

	public void update(float delta) {

		if (!useOverallSpeed) {
			timer += delta;
		} else {
			timer += delta * Locator.getOverallGameSpeed();
		}
		if (timer > CALCULATED_TIME) {
			BackgroundBlockRow b = new BackgroundBlockRow();
			b.makeRow(0);
			rows.add(b);
			timer = 0;
		}
		for (BackgroundBlockRow bbr : rows) {
			bbr.update(delta);
			if (!bbr.getRow().get(0).isExists()) {
				deleted.add(bbr);
			}
		}
		rows.removeAll(deleted);
		deleted.clear();
	}

	public void fillScreen() {
		for (int i = 1; i < 9; i++) {
			BackgroundBlockRow b = new BackgroundBlockRow();
			b.makeRow(i * -5);
			rows.add(b);
		}
	}

	public void restart() {
		rows.clear();
		deleted.clear();
		timer = 3;
		fillScreen();
	}
}
